<template>
    <el-form :model="dataForm"  :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" 
             label-width="150px">
      <div class="tip">收集任务配置</div>
      <el-form-item label="id" prop="id" v-if="dataForm.id" hidden>
        <el-input v-model="dataForm.id" placeholder="id" disabled ></el-input>
      </el-form-item>
       <el-row>
        <el-col :span=24>
          <el-form-item label="任务名称" prop="taskName">
            <el-input v-model="dataForm.taskName" placeholder="任务名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span=12>
          <el-form-item label="cron表达式" prop="jobCron">
            <el-input v-model="dataForm.jobCron" placeholder="cron表达式"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span=12>
          <el-form-item label="重试次数" prop="retryInterval">
            <el-input v-model="dataForm.retryInterval" placeholder="重试次数"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span=24>
          <el-form-item label="申请单" prop="applyInfoId">
            <el-select style="width:100%" v-model="dataForm.applyInfoId" filterable placeholder="申请单" @change="applyInfoChange">
              <el-option v-for="applyInfo in applyInfoList"
              :label="applyInfo.applyDescription"
              :key="applyInfo.applyId"
              :value="applyInfo.applyId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span=10>
          <el-form-item label="收集配置" prop="gatherId">
            <el-select style="width:100%" v-model="dataForm.gatherId" filterable placeholder="收集配置" >
              <el-option v-for="gather in gatherList"
              :label="gather.gatherName"
              :key="gather.gatherId"
              :value="gather.gatherId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span=2>
          <el-button icon="el-icon-plus" type="primary" @click="openAddConfigGather()">新增</el-button>
        </el-col>
        <el-col :span=10>
          <el-form-item label="解码配置" prop="decodeId">
            <el-select style="width:100%" v-model="dataForm.decodeId" filterable placeholder="解码配置">
              <el-option v-for="decode in decodeList"
              :label="decode.decodeName"
              :key="decode.decodeId"
              :value="decode.decodeId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span=2>
          <el-button icon="el-icon-plus" type="primary" @click="openAddDecodeInfo()">新增</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span=12>
          <el-form-item label="执行状态" prop="triggerStatus">
            <el-radio-group v-model="dataForm.triggerStatus">
              <el-radio  :label=0>停止</el-radio>
              <el-radio  :label=1>启动</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span=24>
          <el-form-item label="任务描述" prop="jobDesc">
            <el-input v-model="dataForm.jobDesc" placeholder="任务描述"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
</template>

<script>
  import {getObj, addObj, putObj, findAllGatherInfo, findAllDecodeInfo, findAllApplyInfo} from '@/api/job/gather'

  import {getApplyInfoById} from '@/api/gather/applyinfo'

  export default {
    data() {
      return {
        decodeList: [],
        gatherList: [],
        applyInfoList: [],
        applyInfo: {},
        dataForm: {
          type: '',
          id: '',
          applyInfoId: '',
          gatherId: '',
          decodeId: '',
          checkInfoId: '',
          executorBlockStrategy: '',
          executorFailRetryCount: '',
          executorHandler: '',
          executorParam: '',
          executorRouteStrategy: '',
          executorTimeout: '',
          isAlarm: '',
          jobCron: '',
          jobDesc: '',
          jobGroup: '',
          retryInterval: '',
          taskName: '',
          triggerLastTime: '',
          triggerNextTime: '',
          triggerStatus: '',
        },

        dataRule: {
          type: [
            {required: true, message: 'type不能为空', trigger: 'blur'}
          ],
          executorBlockStrategy: [
            {required: true, message: 'executorBlockStrategy不能为空', trigger: 'blur'}
          ],
          executorFailRetryCount: [
            {required: true, message: 'executorFailRetryCount不能为空', trigger: 'blur'}
          ],
          executorHandler: [
            {required: true, message: 'executorHandler不能为空', trigger: 'blur'}
          ],
          executorParam: [
            {required: true, message: 'executorParam不能为空', trigger: 'blur'}
          ],
          executorRouteStrategy: [
            {required: true, message: 'executorRouteStrategy不能为空', trigger: 'blur'}
          ],
          executorTimeout: [
            {required: true, message: 'executorTimeout不能为空', trigger: 'blur'}
          ],
          isAlarm: [
            {required: true, message: 'isAlarm不能为空', trigger: 'blur'}
          ],
          jobCron: [
            {required: true, message: 'jobCron不能为空', trigger: 'blur'}
          ],
          jobDesc: [
            {required: true, message: 'jobDesc不能为空', trigger: 'blur'}
          ],
          jobGroup: [
            {required: true, message: 'jobGroup不能为空', trigger: 'blur'}
          ],
          retryInterval: [
            {required: true, message: 'retryInterval不能为空', trigger: 'blur'}
          ],
          taskName: [
            {required: true, message: 'taskName不能为空', trigger: 'blur'}
          ],
          triggerLastTime: [
            {required: true, message: 'triggerLastTime不能为空', trigger: 'blur'}
          ],
          triggerNextTime: [
            {required: true, message: 'triggerNextTime不能为空', trigger: 'blur'}
          ],
          triggerStatus: [
            {required: true, message: 'triggerStatus不能为空', trigger: 'blur'}
          ],
          gatherId: [
            {required: true, message: '收集配置不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    components: {
    },
    methods: {
      applyInfoChange (applyInfoId) {
        console.log(this.applyInfoList)
        this.applyInfoList.forEach(applyInfo => {
          if (applyInfo.applyId == applyInfoId) {
            if(applyInfo.applyDecodeId) {
              this.dataForm.decodeId = applyInfo.applyDecodeId
            }
            if (applyInfo.applyConfigGatherId) {
              this.dataForm.gatherId = applyInfo.applyConfigGatherId
            }
          }
        })
      },
      async setDecodeId (decodeId) {
        await this.refreshDecodeInfo()
        this.dataForm.decodeId = decodeId
      },
      async setConfigGather (configGatherId) {
        await this.refreshGatherInfo()
        this.dataForm.gatherId = configGatherId
      },
      openAddConfigGather () {
        this.$emit('openAddConfigGather')
      },
      openAddDecodeInfo () {
        this.$emit('openAddDecodeInfo')
      },
      initByApplyId (applyId) {
        this.init()
        getApplyInfoById(applyId).then(response => {
          if (response.status == 200 && response.data.code == 0) {
            this.dataForm.applyInfoId = applyId
            this.applyInfo = response.data.data
            this.dataForm.decodeId = this.applyInfo.applyDecodeId
            this.gatherDataForm.gatherFtpId = this.applyInfo.applyFtpId
          }
        })
      },
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (id) {
            getObj(id).then(response => {
                this.dataForm = response.data.data
            })
          }
          this.refreshGatherInfo()
          this.refreshDecodeInfo()
          findAllApplyInfo().then(response => {
            if (response.status == 200 && response.data.code == 0) {
              this.applyInfoList = response.data.data
            }
          })

        })
      },
      async refreshDecodeInfo () {
        await findAllDecodeInfo().then(response => {
          if (response.status == 200 && response.data.code == 0) {
            this.decodeList = response.data.data
          }
        })
      },
      async refreshGatherInfo () {
        await findAllGatherInfo().then(response => {
          if (response.status == 200 && response.data.code == 0) {
            this.gatherList = response.data.data
          }else {
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$notify.success('修改成功')
                this.visible = false
                this.$emit('refreshGather', data.data.data.id)
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.success('添加成功')
                this.visible = false
                this.$emit('refreshGather', data.data.data.id)
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
        this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>
